package Client;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

public final class Player {
    private static final int[] DIRECTION_X = { -1, 0, 1, -1, 1, -1, 0, 1 };
    private static final int[] DIRECTION_Y = { 1, 1, 1, 0, 0, -1, -1, -1 };
    private int id;
    private int x;
    private int y;
    private int pixelX;
    private int pixelY;
    private int headId = -1;
    private int torsoId = -1;
    private int legsId = -1;
    private String username = "";
    private String chat;
    private int chatTicks;
    private Sprite appearance;
    private int direction;

    public void setAppearance(int headId, int torsoId, int legsId) {
        this.headId = headId;
        this.torsoId = torsoId;
        this.legsId = legsId;
    }

    public void buildAppearance() {
        if (legsId == -1) {
            return;
        }
        BufferedImage image = new BufferedImage(Sprite.getSprite(legsId).width(), Sprite.getSprite(legsId).height(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) image.createGraphics();
        g.drawImage(Sprite.getSprite(legsId).image(), 0, 0, null);
        g.drawImage(Sprite.getSprite(torsoId).image(), 0, 0, null);
        g.drawImage(Sprite.getSprite(headId).image(), 0, 0, null);
        g.dispose();
        appearance = new Sprite(image);
    }

    public void setDirection(int direction) {
        if (this.direction != direction) {
            this.direction = direction;
            AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
            tx.translate(-appearance.image().getWidth(null), 0);
            AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            appearance = new Sprite(op.filter(appearance.image(), null));
        }
    }

    public void setCoords(int x, int y) {
        this.x = x;
        this.y = y;
        pixelX = x * GameCanvas.TILE_SIZE;
        pixelY = y * GameCanvas.TILE_SIZE;
    }

    public void doChat(String chat) {
        this.chat = chat;
        chatTicks = 2000 / GameCanvas.PROCESS_RATE;
    }

    public void move(int dir) {
        x += DIRECTION_X[dir];
        y += DIRECTION_Y[dir];
    }

    public void tick() {
        if (chatTicks > 0) {
            chatTicks--;
            if (chatTicks == 0) {
                chat = null;
            }
        }
        if (pixelX != x * GameCanvas.TILE_SIZE || pixelY != y * GameCanvas.TILE_SIZE) {
            int toX = x * GameCanvas.TILE_SIZE - pixelX;
            int toY = y * GameCanvas.TILE_SIZE - pixelY;
            int speed = GameCanvas.TILE_SIZE / (600 / GameCanvas.PROCESS_RATE);
            if (toX < 0) {
                toX = Math.max(toX, -speed);
                setDirection(0);
            } else if (toX > 0) {
                toX = Math.min(toX, speed);
                setDirection(1);
            }
            if (toY < 0) {
                toY = Math.max(toY, -speed);
            } else if (toY > 0) {
                toY = Math.min(toY, speed);
            }
            pixelX += toX;
            pixelY += toY;
        }
    }

    public int id() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int x() {
        return x;
    }

    public int y() {
        return y;
    }

    public int pixelX() {
        return pixelX;
    }

    public int pixelY() {
        return pixelY;
    }

    public String username() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String chat() {
        return chat;
    }

    public Sprite appearance() {
        return appearance;
    }
}